package com.aramco.fion.util
{	
	import com.aramco.fion.vo.F4Value;
	import com.aramco.fion.vo.VariableInfo;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.xml.SimpleXMLDecoder;


	public final class QueryUtil
	{
		
		public static function getVariablesScreenMetadata(data:XML):ArrayCollection
		{
			//	if(xml is ResultEvent)
			//		xml = xml.result
			var xmlList:XMLList = data..item;
			var singleVar:VariableInfo;
			var variables:ArrayCollection = new ArrayCollection();
			for each(var node:XML in xmlList)
			{
				singleVar = new VariableInfo();
				singleVar.techname = node.VARI_NAME.toString();
				singleVar.iobjnm = node.OBJ_NAME.toString();
				singleVar.label = node.VARI_TEXT.toString();
				singleVar.defaultValue = node.DEF_VAL.toString();
				//selection type
				switch(node.VARI_PAR_SEL.toString())
				{
					case "S":
						singleVar.selectionType = VariableInfo.SINGLE_SELECTION;
						break;
					case "P":
						singleVar.selectionType = VariableInfo.SINGLE_SELECTION;
						break;
					case "M":
						singleVar.selectionType = VariableInfo.MULTIPLE_SELECTION;
						break;
					case "I":
						singleVar.selectionType = VariableInfo.RANGE_SELECTION;
						break;
					/* case "X":
					singleVar.availableSelection = VariableInfo.INCLUDE_EXCLUDE_SELECTION.toString();
					break; */
					default:
						singleVar.selectionType = VariableInfo.MULTIPLE_SELECTION;
						break;
				} 
				variables.addItem(singleVar);
			}
			return variables;
		}
		
		public static function getVariablesScreenMetadataAviation(data:XML):ArrayCollection
		{
		//	if(xml is ResultEvent)
		//		xml = xml.result
			var xml:XMLList = data.descendants('VARIABLES');
					
			var obj:Object ;
			var metaData:ArrayCollection = new ArrayCollection();
			for each ( var itemNode : Object in xml.descendants('ZBUS_VAR_META'))
			{
				obj = new Object();
				obj.ZVARNAME = itemNode.ZVARNAME.toString();
				obj.ZVAR_DESCRIPTION = itemNode.ZVAR_DESCRIPTION.toString();
				obj.REQUIRED = itemNode.ENTRYTP.toString() != "0"? true:false;
				
				/*Parameter selection: Single value, Interval, Selection.
				P: Single value, S: Selection option, I: Internal, Default, M: Multiple Single Values */
				obj.VPARSEL = itemNode.VPARSEL.toString();
				
				if(itemNode.DEF_VAL.toString() != "")
					obj.DEF_VAL = itemNode.DEF_VAL.toString();
				
				if(itemNode.FLEX_VIEW.toString() != "")
					obj.CLASSNAME = itemNode.FLEX_VIEW.toString();
				/*	else
				obj.CLASSNAME = FlexConstants.DEFAUL_FLEX_COMP;*/
				metaData.addItem(obj);
			}	
			return metaData;
		}
		
		public static function getF4Values(data:XML):ArrayCollection
		{
			var f4Values:ArrayCollection = new ArrayCollection();
			var nodeList:XMLList = data..item;
			var itemElement:F4Value;
			for each(var node:Object in nodeList)
			{
				itemElement = new F4Value(node.CHAVL_EXT, node.CHATEXT.TXTLG);
				f4Values.addItem(itemElement);
			}
			f4Values.refresh();
			return f4Values;
		}
		
		public static function getXMLReport(data:XML):BSPReportDataModelOO
		{
			return new BSPReportDataModelOO(data);
		}
		
		public static function getDashboardTableRecords(data:XML):XMLList
		{
			return (data..VARIABLES).children();
		}
	}
}
